<!--
 * @Author: panghu chenjh@datamargin.com
 * @Date: 2024-03-27 08:52:34
 * @LastEditors: panghu chenjh@datamargin.com
 * @LastEditTime: 2024-03-27 15:13:18
 * @FilePath: \vue3-vite-mall-admin-system\src\components\header.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="header">
    <!-- 左列 -->
    <span class="logo">商城后台管理系统 </span>
    <el-icon class="icon"><Fold @click="handleToCollapse"/></el-icon>
    <!-- 右列 -->
    <div class="f_right">
      <el-tooltip effect="dark" content="刷新" placement="bottom">
        <el-icon class="icon">
          <Refresh @click="refreshFn" />
        </el-icon>
      </el-tooltip>
      <el-tooltip v-if="isFullScreen" effect="dark" content="全屏" placement="bottom">
        <el-icon class="icon">
          <FullScreen @click="toggle" />
        </el-icon>
      </el-tooltip>
      <el-tooltip v-else effect="dark" content="退出全屏" placement="bottom">
        <el-icon class="icon">
          <FullScreen @click="toggle" />
        </el-icon>
      </el-tooltip>
      <el-dropdown @command="handleCommand">
        <span>
          <el-avatar :size="30" :src="$store.state.userInfo.avatar" />
          {{ $store.state.userInfo.username }}
          <el-icon class="el-icon--right" style="margin-left: 10px">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="channgePassword">修改密码</el-dropdown-item>
            <el-dropdown-item command="logOut">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <el-dialog title="修改密码" v-model="dialogFormVisible" width="30%" @close="handleClose">
      <el-form :model="ruleForm" ref="ruleFormRef" :rules="rules" label-width="100px">
        <el-form-item label="旧密码" prop="oldpassword">
          <el-input v-model="ruleForm.oldpassword" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repassword">
          <el-input v-model="ruleForm.repassword" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleOk">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ElMessageBox } from "element-plus";
import { Fold, Refresh, FullScreen, ArrowDown } from "@element-plus/icons"
import { useFullscreen } from '@vueuse/core'
import { useEditPassword } from '@/utils/useEditPassword'

const store = useStore();
const router = useRouter();
const { toggle, isFullScreen } = useFullscreen()
const refreshFn = () => {
  location.reload()
}
// 折叠侧边栏
const handleToCollapse = () => {
  store.commit('SET_ISCOLLAPSE')
}

const handleCommand = (command) => {
  if (command === 'channgePassword') {
    dialogFormVisible.value = true
  }
  if (command === 'logOut') {
    logOut()
  }
}
// 修改密码
const {
  ruleFormRef,
  dialogFormVisible,
  ruleForm,
  rules,
  handleClose,
  handleOk
} = useEditPassword()
// 退出
const logOut = async () => {
  const result = await ElMessageBox.confirm("退出登录?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).catch((err) => err);
  if (result !== "confirm") return;
  sessionStorage.removeItem("token");
  store.commit("SET_USERINFO", {});
  router.push("/login");
};

</script>

<style lang='less' scoped>
.header {
  height: 100%;
  display: flex;
  color: #fff;
  align-items: center;

  .f_right {
    margin-left: auto;
    display: flex;
    align-items: center;
    height: 100%;
    padding-right: 20px;

    .el-dropdown {
      margin-left: 20px;
      color: #fff;
      cursor: pointer;

      span {
        display: flex;
        align-items: center;

        .el-avatar {
          margin-right: 10px;
        }
      }
    }
  }

  .logo {
    font-size: 18px;
    padding-right: 16px;
  }

  .icon {
    width: 30px;
    height: 100%;
    cursor: pointer;
    font-size: 18px;
    font-weight: bold;
  }

  .icon:hover {
    background: #065327;
  }
}

:deep(:focus-visible) {
  outline: none;
}
</style>
